package com.mi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mi.entity.MyPages;
import com.mi.entity.NewsEntity;
import com.mi.entity.ProductCategoryEntity;
import com.mi.entity.UserEntity;
import com.mi.service.NewsService;
import com.mi.service.ProductCategoryService;
import com.mi.service.UserService;
import com.mi.util.MyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Reference(check = false)
    UserService userService;

    @Reference(check = false)
    NewsService newsService;

    @Reference(check = false)
    ProductCategoryService productCategoryService;

    @RequestMapping("logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return "redirect:index";
    }

    //关于安时利
    @RequestMapping("about")
    public String about(){
        return "about";
    }

    //社会责任
    @RequestMapping("social_responsibility")
    public String social(){
        return "social_responsibility";
    }

    //持续发展
    @RequestMapping("sustained_development")
    public String sustained(){
        return "sustained_development";
    }

    //合作伙伴
    @RequestMapping("partner")
    public String partner(){
        return "Partner";
    }


    //首页
    @RequestMapping("index")
    public String index(HttpServletRequest request){
        //查询产品大类别信息
        List<ProductCategoryEntity> bigRanks = productCategoryService.queryProductsRank();
        List littleRanks = new ArrayList();
        for(ProductCategoryEntity pce: bigRanks){   //根据大类别查询小类别信息

            List<ProductCategoryEntity> ranks = productCategoryService.queryProductsInfo(pce);

            littleRanks.add(ranks);
        }
        request.setAttribute("littleRanks",littleRanks);
        request.setAttribute("bigRanks",bigRanks);
        NewsEntity newsEntity1 =  new NewsEntity();
        newsEntity1.setN_category(0);
        MyPages<NewsEntity> newsEntityMyPages =null;
        List<NewsEntity> newsEntities = new ArrayList<>();
        newsEntityMyPages= newsService.queryNews(newsEntity1);
        newsEntities.addAll(newsEntityMyPages.getList());
        newsEntity1.setN_category(1);
        newsEntityMyPages= newsService.queryNews(newsEntity1);
        newsEntities.addAll(newsEntityMyPages.getList());
        newsEntity1.setN_category(2);
        newsEntityMyPages= newsService.queryNews(newsEntity1);
        newsEntities.addAll(newsEntityMyPages.getList());
        System.out.println("查到的个数为："+newsEntities.size());
        System.out.println("要查6条数据的信息为："+newsEntities);
        request.setAttribute("news",newsEntities);
        return "index";
    }

    @RequestMapping("jump")
    public String toLoginPage(){
        return "login";
    }

    //登录
    @ResponseBody
    @RequestMapping("login")
    public Map<String,String> login(UserEntity ue, HttpServletRequest request){

        Map<String, String> map = new LinkedHashMap<>();

        //调用servic层执行登录方法
        UserEntity user = userService.login(ue);

        if(user != null){       //查询结果不为空，登录成功

            request.getSession().setAttribute("user",user);

            map.put("result","0000");

            return map;
        }

        map.put("result","0002");   //查询结果为空，用户名或密码错误

        return map;
    }

    //跳转到注册页面
    @RequestMapping("jumpRegist")
    public String toRegistPage(){
        return "regist";
    }

    //用户名唯一校验
    @ResponseBody
    @RequestMapping("uniqueName")
    public Map<String,String> uniqueName(String uname){
        
        Map<String,String> map = new LinkedHashMap<>();

        //用户名不能为空
        if(MyUtil.isEmpty(uname)){

            map.put("result","0001");

            return map;

        }

        //调用service层方法校验用户名是否存在
        UserEntity user = userService.uniqueName(uname);

        if(null == user){       //用户名可用

            map.put("result","0000");

            return map;
        }

        map.put("result","0002");   //用户名已存在

        return map;
        
    }
    
    //注册
    @ResponseBody
    @RequestMapping("regist")
    public Map<String,String> regist(UserEntity ue){

        Map<String,String> map = new LinkedHashMap();

        //调用service执行注册方法
        boolean b = userService.regist(ue);

        if(b){     //注册成功
            map.put("result","0000");
            return map;
        }
        //注册失败！
        map.put("result","0002");
        return map;
    }


}
